//
//  MineViewController.m
//  海角音乐
//
//  Created by dllo on 15/8/4.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "MineViewController.h"

@interface MineViewController ()
{
    NSString * _cleanTitle;
    NSString *_MM;
    CGFloat floatSize;
}
@property (nonatomic,retain)UIImageView * im1;
@property (nonatomic,retain)UIImageView * im2;

@end

@implementation MineViewController

- (void)dealloc
{
    [_im1 release];
    [_im2 release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64)  style:UITableViewStylePlain];
    [_tableview setBackgroundColor:[UIColor whiteColor]];
    [_tableview setDelegate:self];
    [_tableview setDataSource:self];
    [self.view addSubview:_tableview];
    
    
    
    CGFloat im_height = SCREEN_WIDTH*0.53333; // 200
    // 下拉放大
    // ----------------底
    self.im1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, im_height)];
    _tableview.tableHeaderView = _im1;
    

    self.im2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, im_height)];
    self.im2.image = [UIImage imageNamed:@"模糊页面2.jpg"];
    [self.im1 addSubview:self.im2];
    
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
    footerView.alpha = 0.3;
    footerView.backgroundColor = [UIColor lightGrayColor];
    _tableview.tableFooterView = footerView;
    [footerView release];
    
    
}

// 下拉效果
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 改变垂直偏移量
    CGFloat yy = scrollView.contentOffset.y;
    if (yy <= 0) {
        // 取正值
        yy = 0 - yy;
        
        CGFloat im2_height = SCREEN_WIDTH*0.53333; // 200
        self.im2.frame = CGRectMake(0 - yy , 0 - yy, self.view.frame.size.width + yy + yy, im2_height + yy);
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row == 0) {
        
        MyCollectMusicViewController *myCollectVc = [[MyCollectMusicViewController alloc] init];
        [self.navigationController pushViewController:myCollectVc animated:YES];
        
    } else if (indexPath.row == 1) {
        
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"清除成功" message:nil delegate:self cancelButtonTitle:@"返回" otherButtonTitles:@"确认", nil];
        [alert show];
    } else if (indexPath.row == 2) {
        
        
        
    } else {        
        AboutViewController *abouVc = [[[AboutViewController alloc] init] autorelease];
        [self.navigationController pushViewController:abouVc animated:YES];
    }
    [_tableview reloadData];
    
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellId = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == NO) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
    }
    
    if (indexPath.row == 0) {
        
        cell.textLabel.text = @"我的收藏";
    } else if (indexPath.row == 1) {
        
        cell.textLabel.text = @"清除缓存";
        
        cell.detailTextLabel.text = _cleanTitle;
        
    } else if (indexPath.row == 2) {
        
        cell.textLabel.text = @"夜间模式";
    } else if (indexPath.row == 3) {
        
        cell.textLabel.text = @"关于海角";
    }
    
    
    _cleanTitle = nil;
    NSUInteger size = [SDImageCache sharedImageCache].getSize;
    if (size > 1024 * 1024) {
        floatSize = size / 1024.0 / 1024.0;
        CGFloat sum = floatSize+[self filePath];
        _cleanTitle = [NSString stringWithFormat :@"%.2fM" , sum];
        
    }else if (size > 1024){
       floatSize = size / 1024.0;
        CGFloat sum = floatSize+[self filePath];
        _cleanTitle = [NSString stringWithFormat :@"%.2fM" , sum];
        
    }else if (size > 0){
        CGFloat sum = size+[self filePath];
        _cleanTitle = [NSString stringWithFormat :@"%.2fM" , sum];
    }else
    {
        CGFloat sum = [self filePath];
        _cleanTitle = [NSString stringWithFormat :@"%.2fM" , sum];
    }
    
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    
    
    
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return SCREEN_WIDTH*0.16;
  
}


- (long long) fileSizeAtPath:( NSString *) filePath{
    
    NSFileManager * manager = [ NSFileManager defaultManager];
    if ([manager fileExistsAtPath :filePath]){
        return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize ];
    }
    return 0 ;
    
}
- ( float ) folderSizeAtPath:( NSString *) folderPath{
    
    NSFileManager * manager = [ NSFileManager defaultManager ];
    if (![manager fileExistsAtPath :folderPath]) return 0 ;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator ];
    NSString * fileName;
    long long folderSize = 0 ;
    while ((fileName = [childFilesEnumerator nextObject ]) != nil ){
        NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
        folderSize += [ self fileSizeAtPath :fileAbsolutePath];
    }
    return folderSize/( 1024.0 * 1024.0 );
    
}
- ( float )filePath
{
    
    NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];    
    return [ self folderSizeAtPath :cachPath];
    
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        NSLog(@"取消");
    } else {
        
        [[SDImageCache sharedImageCache] clearDisk];
        
        NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
        
        NSArray * files = [[ NSFileManager defaultManager ] subpathsAtPath :cachPath];
        for ( NSString * p in files) {
            NSError * error = nil ;
            NSString * path = [cachPath stringByAppendingPathComponent :p];
            
            if ([[ NSFileManager defaultManager ] fileExistsAtPath :path]) {
                
                [[ NSFileManager defaultManager ] removeItemAtPath :path error :&error];
            }
        }
        [_tableview reloadData ];//清理完之后重新导入数据
    }
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
